<template>
  <a-modal
    class="addProduct"
    :visible="visible"
    title="编辑商品"
    :maskClosable="false"
    :destroyOnClose="true"
    :keyboard="false"
    :closable="true"
    width="900px"
    @cancel="closeModal()"
    @ok="handleSubmit"
  >
    <a-form-model
      class="group-bargaining-form"
      ref="ruleForm"
      :model="formData"
      :rules="rules"
      :label-col="{ span: 5 }"
      :wrapper-col="{ span: 19 }"
    >
      <a-form-model-item label="剩余数量" prop="updateRemainCount">
        <a-input-number
          v-model="formData.updateRemainCount"
          :min="0"
          :step="1"
          :precision="0"
        />
        份
      </a-form-model-item>
      <a-form-model-item label="中奖概率" prop="odds">
        <a-input-number
          v-model="formData.odds"
          :min="0.0"
          :step="0.01"
          :precision="2"
          :max="maxOdds"
        />
        %
        <div style="line-height: 20px; margin-top: 5px">
          中奖概率为用户每次抽奖时独立事件的概率。举例：若中奖率为10%，<br />则每个用户中奖的概率为10%，并不是10个人中一定出现1个奖品<br />全部商品中奖概率之和不能大于100%
        </div>
      </a-form-model-item>
    </a-form-model>
    <template slot="footer">
      <a-button key="back" @click="closeModal"> 取消 </a-button>
      <a-button key="submit" type="primary" @click="handleSubmit">
        确定
      </a-button>
    </template>
  </a-modal>
</template>

<script>
export default {
  name: "updatePrize",
  components: {},
  props: {
    editData: {
      type: Object,
      default: {},
    },
    tableData: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      visible: false,
      formData: {},
      rules: {
        updateRemainCount: [
          {
            required: true,
            message: "请输入剩余数量",
            trigger: "blur",
          },
        ],
        odds: [
          {
            required: true,
            message: "请输入抽奖概率",
            trigger: "blur",
          },
        ],
      },
      maxOdds: 100,
    };
  },
  methods: {
    // 确定提交
    closeModal() {
      this.visible = false;
    },
    handleSubmit() {
      console.log(this.formData);
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // alert("submit!");
          // this.$emit("comfirmGiftInfo", params, "update", this.goodsIndex);
          const prizeItem = {};
          if (this.editData.prizeType == 1) {
            prizeItem.prizeType = this.editData.prizeType * 1;
            prizeItem.relationId = this.editData.relationId;
            prizeItem.prizeName = this.editData.prizeName;
            prizeItem.amount = this.editData.amount;
            prizeItem.issueCount = this.editData.issueCount;
            prizeItem.activityId =
              this.editData.activityId || this.$route.query.id;
            prizeItem.id = this.editData.id;
            prizeItem.remainingStock = this.editData.remainingStock;
            prizeItem.updateRemainCount = this.formData.updateRemainCount;
            prizeItem.odds = this.formData.odds;
          } else if (this.editData.prizeType == 2) {
            prizeItem.prizeType = this.editData.prizeType * 1;
            prizeItem.prizeName = this.editData.prizeName;
            prizeItem.amount = this.editData.amount;
            prizeItem.issueCount = this.editData.issueCount;
            prizeItem.integralCount = this.editData.integralCount;
            prizeItem.activityId =
              this.editData.activityId || this.$route.query.id;
            prizeItem.remainingStock = this.editData.remainingStock;
            prizeItem.id = this.editData.id;
            prizeItem.updateRemainCount = this.formData.updateRemainCount;
            prizeItem.odds = this.formData.odds;
          } else if (this.editData.prizeType == 3) {
            prizeItem.prizeType = this.editData.prizeType * 1;
            prizeItem.prizeName = this.editData.prizeName;
            prizeItem.amount = this.editData.amount;
            prizeItem.issueCount = this.editData.issueCount;
            prizeItem.relationId = this.editData.relationId;
            prizeItem.validDay = this.editData.validDay;
            prizeItem.goodsType = this.editData.goodsType;
            prizeItem.activityId =
              this.editData.activityId || this.$route.query.id;
            prizeItem.remainingStock = this.editData.remainingStock;
            prizeItem.id = this.editData.id;
            prizeItem.updateRemainCount = this.formData.updateRemainCount;
            prizeItem.odds = this.formData.odds;
          }
          this.editData.updateRemainCount = prizeItem.updateRemainCount;
          this.editData.odds = prizeItem.odds;
          this.updatePrizePool(prizeItem);
        } else {
          this.$message.error("缺少必填信息");
          return false;
        }
      });
    },

    async updatePrizePool(params) {
      let res = await this.$post(
        "goldenhome/gh/lottery/admin_/lottery/activity/prize/pool/update",
        params
      );
      if (res && res.returnCode == 1000) {
        // console.log(res);
        this.visible = false;
        this.$message.success("修改奖品信息成功");
        this.$emit("comfirmUpdatePrize", this.editData);
      }
    },
  },
  watch: {
    visible(n) {
      if (n) {
        this.formData = {
          updateRemainCount: this.editData.amount - this.editData.chooseAmount,
          odds: this.editData.odds,
        };
        this.maxOdds = 100;
        let num = 0;
        this.tableData.forEach((item) => {
          num = num + item.odds;
        });
        this.maxOdds = this.maxOdds - num + this.editData.odds;
      } else {
        this.formData = {};
      }
    },
  },
};
</script>

<style scoped lang="less">
</style>

